package org.openkoala.koala.popup.javaclass.crud;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.openkoala.koala.deploy.curd.module.core.EntityModel;
import org.openkoala.koala.deploy.curd.module.core.FieldModel;
import org.openkoala.koala.deploy.curd.module.core.model.RelationFieldModel;
import org.openkoala.koala.deploy.curd.module.ui.UIWidget;
import org.openkoala.koala.deploy.curd.module.ui.model.TabModel;
import org.openkoala.koala.deploy.curd.module.ui.model.TabViewUI;
import org.openkoala.koala.deploy.curd.module.ui.model.ViewModel;

public class RelationFieldConfigDialog extends Dialog {

	private RelationViewComponent parent;
	
	private Map<FieldModel, List<FieldModel>> relationFieldModels;
	
	private EntityModel entityModel;
	private ViewModel viewModel;
	
	private Composite mainComposite;
	private Combo relativeFieldsCombo;
	private Text viewText;
	
	private Composite fieldComposite;
	private Combo fieldsCombo;

//	private Composite relativeFieldExpressComposite;
//	private Combo relativeFieldExpressCombo;
	
	private GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);

	private RelationFieldConfigDialog(Shell shell, RelationViewComponent parent, Map<FieldModel, List<FieldModel>> relationFieldModels, 
			ViewModel viewModel, EntityModel entityModel) {
		super(shell);
		this.parent = parent;
		this.relationFieldModels = relationFieldModels;
		this.viewModel = viewModel;
		this.entityModel = entityModel;
	}
	
	public static RelationFieldConfigDialog createInstance(Shell shell, RelationViewComponent parent, 
			 Map<FieldModel, List<FieldModel>> relationFieldModels, ViewModel viewModel, EntityModel entityModel) {
		return new RelationFieldConfigDialog(shell, parent, relationFieldModels, viewModel, entityModel);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Edit Configuration");
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		mainComposite = new Composite(parent, SWT.NONE);
		initializeDialogUnits(mainComposite);

		GridData mainGridData = new GridData(250, 150);
		mainComposite.setLayoutData(mainGridData);

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		mainComposite.setLayout(gridLayout);

		generateConfigComposite(mainComposite);

		return parent;
	}
	
	private void generateConfigComposite(Composite parentComposite) {
		GridData gridData = new GridData(GridData.FILL_BOTH);
		GridLayout gridLayout = new GridLayout(2, false);
		
		Composite composite = new Composite(parentComposite, SWT.NONE);
		composite.setLayout(gridLayout);
		composite.setLayoutData(gridData);

		if (relationFieldModels != null) {
			Label fieldLable = new Label(composite, SWT.NONE);
			fieldLable.setText("属性");
			relativeFieldsCombo = new Combo(composite, SWT.READ_ONLY);
			for (FieldModel fieldModel : relationFieldModels.keySet()) {
				relativeFieldsCombo.add(fieldModel.getName());
			}
			relativeFieldsCombo.addSelectionListener(new RelationFieldsComboSelectionListener());
		}
	}
	
	private void createFieldsComposite(Composite parentComposite, List<FieldModel> fieldModels) {
		GridData gridData = new GridData(GridData.FILL_BOTH);
		GridLayout gridLayout = new GridLayout(2, false);
		
		fieldComposite = new Composite(parentComposite, SWT.NONE);
		fieldComposite.setLayout(gridLayout);
		fieldComposite.setLayoutData(gridData);

		Label expressLabel = new Label(fieldComposite, SWT.NONE);
		expressLabel.setText("关联属性");
		fieldsCombo = new Combo(fieldComposite, SWT.READ_ONLY);
		
		addItemsToExpressCombo(fieldModels);
		
//		fieldsCombo.addSelectionListener(new FieldsComboSelectionListener());
		
		Label viewLabel = new Label(fieldComposite, SWT.NONE);
		viewLabel.setText("显示名称");
		
		viewText = new Text(fieldComposite, SWT.BORDER);
		viewText.setLayoutData(textGridData);
	}
	
//	private void createRelationFieldExpressComposite(Composite parentComposite, String fieldName, List<FieldModel> fieldModels) {
//		GridData gridData = new GridData(GridData.FILL_BOTH);
//		GridLayout gridLayout = new GridLayout(2, false);
//		
//		relativeFieldExpressComposite = new Composite(parentComposite, SWT.NONE);
//		relativeFieldExpressComposite.setLayout(gridLayout);
//		relativeFieldExpressComposite.setLayoutData(gridData);
//
//		Label label = new Label(relativeFieldExpressComposite, SWT.NONE);
//		label.setText("关联属性的关联属性");
//		relativeFieldExpressCombo = new Combo(relativeFieldExpressComposite, SWT.READ_ONLY);
//		addRelationFieldExpressComboItems(fieldName, fieldModels);
//	}

//	private void addRelationFieldExpressComboItems(String fieldName, List<FieldModel> fieldModels) {
//		RelationFieldModel relationFieldModel = (RelationFieldModel) CrudUtils.getFieldModelByName(fieldName, fieldModels);
//		EntityModel entityModel = relationFieldModel.getRelationModel().getEntityModel();
//		List<FieldModel> associationFieldModels = new ArrayList<FieldModel>(entityModel.getFields());
//		associationFieldModels.remove(entityModel.getObjectValueFieldModel());
//		associationFieldModels.remove(entityModel.getRelationFieldModel());
//		for (FieldModel fieldModel : associationFieldModels) {
//			relativeFieldExpressCombo.add(fieldModel.getName());
//		}
//	}
	
//	private void changeRelationFieldExpressComboData(String fieldName, List<FieldModel> fieldModels) {
//		fieldsCombo.removeAll();
//		addRelationFieldExpressComboItems(fieldName, fieldModels); 
//	}

	private void addItemsToExpressCombo(List<FieldModel> fieldModels) {
		for (FieldModel fieldModel : fieldModels) {
			fieldsCombo.add(fieldModel.getName());
		}
	}
	
	private void changeExpressComboData(List<FieldModel> fieldModels) {
		fieldsCombo.removeAll();
		addItemsToExpressCombo(fieldModels); 
	}
	
	@Override
	protected void okPressed() {
		if (!validate()) {
			return;
		}
		
		addField();
		super.okPressed();
	}

	private void addField() {
		String relativeFieldName = relativeFieldsCombo.getText();
		RelationFieldModel relativeField = (RelationFieldModel) CrudUtils.getFieldModelByName(relativeFieldName, relationFieldModels.keySet());
		UIWidget widget = generateWidget(relativeField);
		TabViewUI tabViewUI = CrudUtils.getTabViewUiFromModel(relativeField, viewModel);
		
		if (tabViewUI == null) {
			TabModel listModel = null;
			if (fieldsCombo != null) {
				List<UIWidget> views = new ArrayList<UIWidget>();
				views.add(widget);
				listModel = new TabModel(views);
			}
			tabViewUI = new TabViewUI(entityModel, relativeField, listModel);
			viewModel.getTabs().add(tabViewUI);
		} else {
			tabViewUI.getListModel().getViews().add(widget);
		}
		
		parent.addItem(widget, tabViewUI);
	}

	private UIWidget generateWidget(RelationFieldModel relativeField) {
		FieldModel fieldModel = CrudUtils.getFieldModelByName(fieldsCombo.getText(), relationFieldModels.get(relativeField));
//		if (fieldModel instanceof RelationFieldModel) {
//			String express = relativeFieldExpressCombo.getText();
//			List<FieldModel> fieldModels = ((RelationFieldModel) fieldModel).getRelationModel().getEntityModel().getFields();
//			return CrudUtils.createRelativeReadOnlyView(fieldModel.getName(), viewText.getText().trim(), express, 
//					CrudUtils.getFieldModelByName(relativeFieldExpressCombo.getText(), fieldModels).getType());
//		} else {
			return CrudUtils.createReadOnlyView(fieldModel.getName(), viewText.getText().trim(), fieldModel.getName(), fieldModel.getType());
//		}
	}
	
	private boolean validate() {
		if (relativeFieldsCombo != null && relativeFieldsCombo.getText().isEmpty()) {
			MessageDialog.openError(getShell(), "错误提示！", "请选择属性！");
			return false;
		}
		if (fieldsCombo != null && fieldsCombo.getText().isEmpty()) {
			MessageDialog.openError(getShell(), "错误提示！", "请选择关联属性！");
			return false;
		}
//		if (relativeFieldExpressCombo != null && relativeFieldExpressCombo.getText().isEmpty()) {
//			MessageDialog.openError(getShell(), "错误提示！", "请选择关联属性的关联属性！");
//			return false;
//		}
		if (viewText.getText().isEmpty()) {
			MessageDialog.openError(getShell(), "错误提示！", "请填写显示名称！");
			return false;
		}
		return true;
	}
	
	private class RelationFieldsComboSelectionListener implements SelectionListener {
		@Override
		public void widgetSelected(SelectionEvent e) {
			List<FieldModel> fieldModels = 
					relationFieldModels.get(CrudUtils.getFieldModelByName(relativeFieldsCombo.getText(), relationFieldModels.keySet()));
			if (fieldsCombo == null && fieldModels != null) {
				createFieldsComposite(mainComposite, fieldModels);
				mainComposite.layout();
			} else if (fieldsCombo != null && fieldModels != null) {
				changeExpressComboData(fieldModels);
			} else if (fieldsCombo != null) {
				fieldsCombo = null;
				fieldComposite.dispose();
			}
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
		}
	}
//	
//	private class FieldsComboSelectionListener implements SelectionListener {
//		@Override
//		public void widgetSelected(SelectionEvent e) {
//			List<FieldModel> fieldModels = 
//					relationFieldModels.get(CrudUtils.getFieldModelByName(relativeFieldsCombo.getText(), relationFieldModels.keySet()));
//			String fieldName = fieldsCombo.getText();
//			FieldModel fieldModel = CrudUtils.getFieldModelByName(fieldName, fieldModels);
//			if (fieldModel instanceof RelationFieldModel && relativeFieldExpressCombo == null && fieldName != null) {
//				createRelationFieldExpressComposite(mainComposite, fieldsCombo.getText(), fieldModels);
//				mainComposite.layout();
//			} else if (fieldModel instanceof RelationFieldModel && relativeFieldExpressCombo != null && fieldName != null) {
//				changeRelationFieldExpressComboData(fieldName, fieldModels);
//			} else if (!(fieldModel instanceof RelationFieldModel) && relativeFieldExpressCombo != null) {
//				relativeFieldExpressCombo = null;
//				relativeFieldExpressComposite.dispose();
//			}
//		}
//
//		@Override
//		public void widgetDefaultSelected(SelectionEvent e) {
//		}
//	}
	
}
